package gx.web.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import gx.entity.Category;
import gx.entity.Manager;
import gx.entity.News;
import gx.entity.NewsType;
import gx.entity.Product;
import gx.entity.News;
import gx.entity.common.Page;
import gx.service.NewsService;
import gx.service.NewsTypeService;
//TODO
//其他人员只能查看新闻，不能修改删除操作新闻
//新闻发布者才能对新闻进行管理
@Controller
@RequestMapping("/mng/news")
public class NewsController {
	@Autowired
	private NewsService newsService;
	@Autowired
	private NewsTypeService newsTypeService;
	

@RequestMapping("/list.jhtml")
//根据ID查询详情
public ModelAndView list(@RequestParam(name="size",defaultValue="10")int size,
		@RequestParam(name="number",defaultValue="1")int number){
		ModelAndView mav=new ModelAndView("/mng/news/list");
		Page<News> page=newsService.findByPager(size, number);
		
		mav.addObject("page", page);
		return mav;
		
}
//根据ID查询内容
@RequestMapping("/content.jhtml")
public ModelAndView content(Integer id){
		ModelAndView mav=new ModelAndView("/mng/news/content");
		News news=newsService.get(id);
		mav.addObject("news",news);
//		List<NewsType> list=newsTypeService.findAll();
//		mav.addObject("newsType",list);
		return mav;
}


//打开新增页面时，预加载商品分类下拉列表
@RequestMapping("/toAdd.jhtml")
public ModelAndView toAdd(){
	ModelAndView mav=new ModelAndView("/mng/news/add");
	List <NewsType> list= newsTypeService.findAll();
	mav.addObject("news_cates", list);
	return mav;
}
//新增
@RequestMapping("/add.jhtml")
public String add(Integer news_cate,News news,HttpSession session){
	Manager wirter= (Manager) session.getAttribute("curr_mgr");
	NewsType newsType=newsTypeService.get(news_cate);
	news.setNews_date(new Date());
	news.setWirter(wirter.getLname());
	news.setNewsType(newsType);
	newsService.save(news);
	return "redirect:/mng/news/list.jhtml";
}


// 根据ID查询详情
@RequestMapping("/detail.jhtml")
public ModelAndView detail(Integer id) throws Exception {
	ModelAndView mav = new ModelAndView("/mng/news/detail");
	News news = newsService.get(id);
	mav.addObject("news", news);
	List<NewsType> list=newsTypeService.findAll();
	mav.addObject("newsType",list);
	return mav;
}

//根据Id修改
@RequestMapping("/edit.jhtml")
public String edit(Integer id,String title,String content,Integer type_id,String wirter){
	System.out.println("=============================================");
	System.out.println(id);
	News news=newsService.get(id);
	System.out.println(news.getId());
	news.setNewsType(newsTypeService.get(type_id));
	news.setTitle(title);
	news.setContent(content);
	news.setWirter(wirter);
	newsService.update(news);
	
	return "redirect:/mng/news/list.jhtml";
}
// 删除
	@RequestMapping("/delete.jhtml")
	public String delete(Integer id){
		newsService.delete(id);
		return "redirect:/mng/news/list.jhtml";
	}
	// 模糊查询
//	@RequestMapping("/search.jhtml")
//	public ModelAndView  Search(String condition)throws Exception{
//		ModelAndView mav=new ModelAndView("mng/news/list");//?
//		List <News> list=newsService.findByName(condition);
//		mav.addObject("page",list);
//		return mav;
//	}
	
	//按名称查询
	@RequestMapping("/search.jhtml")
	public ModelAndView find(
			@RequestParam(name = "size", defaultValue = "10") int size,
			@RequestParam(name = "number", defaultValue = "1") int number,
			String condition){
		ModelAndView mav=new ModelAndView("/mng/news/list");
		Page<News> page=newsService.findByName(size,number,condition);
		mav.addObject("page",page);
		return mav;
	}
	
	
	
	
	
	
}
